package cn.jeefast.controller;


import cn.jeefast.common.utils.BillNumberBuilder;
import cn.jeefast.common.utils.DateUtil;
import cn.jeefast.common.utils.DateUtils;
import cn.jeefast.entity.Aaa;
import cn.jeefast.resthz.webservice.entity.CapitalJournal;
import cn.jeefast.resthz.webservice.entity.CarFreeCarrier;
import cn.jeefast.resthz.webservice.service.CarWebService;
import cn.jeefast.service.AaaService;
import cn.jeefast.util.ExcelUtil;
import com.alibaba.fastjson.JSON;
import io.swagger.models.auth.In;
import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import static cn.jeefast.common.utils.DateUtils.OREDER_PRE_TIME;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yaodongyun
 * @since 2018-11-11
 */
@RestController
@RequestMapping("/aaaController")
public class AaaController {
    @Resource
    private AaaService aaaService;
    @Resource
    private CarWebService carWebService;

    @RequestMapping("/insert")
    public String shangchuan(){
        String num;
        boolean flag=false;
        //查询数据库中的待上传数据
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("type",0);
        List<Aaa> list=aaaService.selectByMap(map);
        System.out.println("List.size()="+list.size());
        Aaa aaa=new Aaa();
        //运单
        CarFreeCarrier cfc = new CarFreeCarrier();
        cfc.setDocumentName("无车承运人电子路单");
        cfc.setDocumentVersionNumber("2015WCCYR");
        cfc.setSenderCode("21205");
        cfc.setRecipientCode("hbjc00001");
        cfc.setMessageSendingDateTime(DateUtil.getCurrentDateTime(13));
        cfc.setMessageFunctionCode("9");
        cfc.setCarrier("河北多拉拉物流有限公司");
        cfc.setUnifiedSocialCreditIdentifier("91130582MA09BE062A");
        cfc.setCPermitNumber("130582001164");
        cfc.setConsignmentDateTime(DateUtil.getCurrentDateTime(13));
        cfc.setBusinessTypeCode("1002996");
        cfc.setRemark("人民币");
                   //件数
        cfc.setTotalNumberOfPackages("1");
        //资金流水单
        CapitalJournal c = new CapitalJournal();
        c.setDocumentName("资金流水单");
        c.setDocumentVersionNumber("2015WCCYR");
        c.setSenderCode("21205");
        c.setRecipientCode("hbjc00001");
        c.setMessageSendingDateTime(DateUtils.format(new Date(),OREDER_PRE_TIME));
        c.setMessageFunctionCode("9");
        //支付方式
        c.setPaymentMeansCode("42");
        for (int i=0;i<list.size();i++){
            if(list.get(i).getDespatchActualDateTime().intValue()>list.get(i).getGoodsReceiptDateTime().intValue()){
                System.out.println("装车时间大于卸车时间，id"+list.get(i).getId());
                continue;
            }
            cfc.setOriginalDocumentNumber(list.get(i).getShippingNoteNumber());
            cfc.setShippingNoteNumber(list.get(i).getShippingNoteNumber());
            //装货地点
            cfc.setPlaceOfLoading(list.get(i).getPlaceOfLoading());
            //国家行政区代码
            cfc.setCCountrySubdivisionCode(list.get(i).getcCountrySubdivisionCode());
            //收货地址
            cfc.setGoodsReceiptPlace(list.get(i).getGoodsReceiptPlace());
            //国家行政区代码
            cfc.setDCountrySubdivisionCode(list.get(i).getdCountrySubdivisionCode());
            cfc.setLicensePlateTypeCode(list.get(i).getLicensePlateTypeCode());
            cfc.setVehicleClassificationCode(list.get(i).getVehicleClassificationCode());
            cfc.setVehicleTonnage(list.get(i).getVehicleTonnage());
            cfc.setDescriptionOfGoods(list.get(i).getDescriptionOfGoods());
            cfc.setCargoTypeClassificationCode(list.get(i).getCargoTypeClassificationCode());
            cfc.setCube(list.get(i).getCube());
            //货物装车后的发车时间
            cfc.setDespatchActualDateTime(list.get(i).getDespatchActualDateTime()+"");
            //收货时间
            cfc.setGoodsReceiptDateTime(list.get(i).getGoodsReceiptDateTime()+"");
            //钱
            cfc.setTotalMonetaryAmount(list.get(i).getTotalMonetaryAmount());
            cfc.setGoodsItemGrossWeight(list.get(i).getGoodsItemGrossWeight());
            cfc.setVehicleNumber(list.get(i).getVehicleNumber());
            cfc.setNameOfPerson(list.get(i).getNameOfPerson());
            //道路运输证号
            cfc.setRoadTransportCertificateNumber(list.get(i).getRoadTransportCertificateNumber());

            num = BillNumberBuilder.makeOrderNum();
            c.setDecumentNumber(num);
            c.setLicensePlateTypeCode(list.get(i).getLicensePlateTypeCode());
            //托运单号
            c.setShippingNoteNumber(list.get(i).getShippingNoteNumber());
            c.setVehicleNumber(list.get(i).getVehicleNumber());
            c.setCarrier(list.get(i).getNameOfPerson());
            c.setSequenceCode(list.get(i).getSequenceCode());
            c.setMonetaryAmount(list.get(i).getMonetaryAmount());
            c.setDateTime(list.get(i).getDateTime());
            try{
                if(list.get(i).getType()==0){
                    flag=carWebService.uploadCarFreeCarrier(cfc).isSendResult();
                    if(flag==true){     //上传成功后，改变数据库type为1，代表运单上传成功。
                        aaa.setId(list.get(i).getId());
                        aaa.setType(1);
                        aaaService.updateById(aaa);
                        flag=carWebService.uploadCapitalJournal(c).isSendResult();
                    }
                }else if(list.get(i).getType()==1){
                    flag=carWebService.uploadCapitalJournal(c).isSendResult();
                }


                if(flag==true){
                    aaa.setId(list.get(i).getId());
                    aaa.setType(2);
                    aaaService.updateById(aaa);
                }
                if(flag==false){
                    System.out.println("错误id:      "+list.get(i).getId());

                }
                flag=false;
            }catch (Exception e ){
                System.out.println(e);
                flag=false;
                continue;
            }


            //资金流水单
        }

        return "已完成操作";
    }


//    @RequestMapping("/haowangjiao")
//    public String haowangjiao() throws  Exception{
//        String
//                path="C:\\Users\\Wade\\Desktop\\job\\资金流水单+-+好望角20181117.xlsx";
//        Boolean flag=false;
//        CapitalJournal c = new CapitalJournal();
//        List<List<String>> returnList = ExcelUtil.readExcelWithSheet(new File(path),0);
//        System.out.println(returnList.size());
//        for(int i=1;i<returnList.size();i++){
//            if(returnList.get(i).get(0)==null){
//                continue;
//            }
//            c.setDocumentName(returnList.get(i).get(0));
//            c.setDocumentVersionNumber(returnList.get(i).get(1));
//            if(returnList.get(i).get(2).contains(".")){
//                String string[]=returnList.get(i).get(2).split("\\.");
//                c.setSenderCode(string[0]);
//            }else {
//                c.setSenderCode(returnList.get(i).get(2));
//            }
//            c.setRecipientCode(returnList.get(i).get(3));
//            c.setMessageSendingDateTime(DateUtils.format(new Date(),OREDER_PRE_TIME));
//            if(returnList.get(i).get(4).contains(".")){
//                String string[]=returnList.get(i).get(4).split("\\.");
//                c.setMessageFunctionCode(string[0]);
//            }else{
//                c.setMessageFunctionCode(returnList.get(i).get(4));
//            }
//            c.setDecumentNumber(returnList.get(i).get(5));
//            c.setCarrier(returnList.get(i).get(6));
//            c.setVehicleNumber(returnList.get(i).get(7));
//            c.setLicensePlateTypeCode(returnList.get(i).get(8));
//            //托运单号
//            c.setShippingNoteNumber(returnList.get(i).get(9));
//            c.setRemark(returnList.get(i).get(10));
//            //支付方式
//            if(returnList.get(i).get(11).contains(".")){
//                String string[]=returnList.get(i).get(11).split("\\.");
//                c.setPaymentMeansCode(string[0]);
//            }else{
//                c.setPaymentMeansCode(returnList.get(i).get(11));
//            }
//            //银行代码
//            if(returnList.get(i).get(12).contains(".")){
//                String bankCode[]=returnList.get(i).get(12).split("\\.");
//                c.setBankCode(bankCode[0]);
//            }else{
//                c.setBankCode(returnList.get(i).get(12));
//            }
//
//            c.setSequenceCode("20181117002");
//            c.setMonetaryAmount(returnList.get(i).get(14)+"00");
//            c.setDateTime("20181117170406");
//            flag=carWebService.uploadCapitalJournal(c).isSendResult();
//            if(flag==false){
//                System.out.println("错误行:"+(i+1)+"， 牌号："+returnList.get(i).get(7));
//                break;
//            }
//            System.out.println("已上传完成："+i+"条数据");
//            //支付方式
//        }
//        return "Success";
//    }
    //保存到数据库
    @RequestMapping("/impPriceRecord2")
    public String impPriceRecord() throws Exception {
        Aaa aa=new Aaa();
        String num;
        String zhuangchetime;
        int integer;
        String path="";

        try {
            path = "C:\\Users\\Wade\\Desktop\\job\\四月份安全煤总明细.xlsx";
            List<List<String>> returnList = ExcelUtil.readExcelWithSheet(new File(path),0);
            integer=returnList.size();
            System.out.println(returnList.size());
         //   System.out.println(JSON.toJSONString(returnList));
            for(int i=1;i<returnList.size();i++){
                 num = BillNumberBuilder.makeOrderNum();
                //装车时间
             //   zhuangchetime=returnList.get(i).get(0);
             //   integer=Long.valueOf(zhuangchetime);
             //   System.out.println("装车时间="+integer);
                //非空验证
                if(returnList.get(i).get(0)==null){
                    continue;
                }
                aa.setDespatchActualDateTime(Long.valueOf(returnList.get(i).get(0)));
                //卸车时间
                //zhuangchetime=returnList.get(i).get(1);
                //integer=Long.valueOf(zhuangchetime);
                if(returnList.get(i).get(1).contains("+")){
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
                   Date date=sdf.parse(returnList.get(i).get(0));
                   Calendar calendar=Calendar.getInstance();
                   calendar.setTime(date);
                   calendar.add(Calendar.DATE,1);
                    String dateStr = sdf.format(calendar.getTime());
                    aa.setGoodsReceiptDateTime(Long.valueOf(dateStr));
                }else{
                    aa.setGoodsReceiptDateTime(Long.valueOf(returnList.get(i).get(1)));
                }
                //装车地点
                aa.setPlaceOfLoading(returnList.get(i).get(2));
                //装车地点行政区代码
                if(returnList.get(i).get(3).contains(".")){
                    String asd[]=returnList.get(i).get(3).split("\\.");
                    aa.setcCountrySubdivisionCode(asd[0]);
                }else{
                    aa.setcCountrySubdivisionCode(returnList.get(i).get(3));
                  //  System.out.println("此数据不包含.！！！！！！！！！！！！！！");
                }
                //卸车地点
                aa.setGoodsReceiptPlace(returnList.get(i).get(4));
                //卸车地点行政区代码
                if(returnList.get(i).get(5).contains(".")){
                    String endXingzheng[]=returnList.get(i).get(5).split("\\.");
                    aa.setdCountrySubdivisionCode(endXingzheng[0]);
                }else{
                    aa.setdCountrySubdivisionCode(returnList.get(i).get(5));
                }
                //货主给的钱
                if(returnList.get(i).get(6).length()<=7){
                    aa.setTotalMonetaryAmount(returnList.get(i).get(6)+"00");
                }else{
                    aa.setTotalMonetaryAmount(returnList.get(i).get(6)+"");
                }
                //车牌号
                aa.setVehicleNumber(returnList.get(i).get(7));
                //司机
                aa.setNameOfPerson(returnList.get(i).get(8));
                //牌照类型
                if(returnList.get(i).get(9).equals("黄牌")){
                    aa.setLicensePlateTypeCode("01");
                }else if(returnList.get(i).get(9).equals("蓝牌")){
                    aa.setLicensePlateTypeCode("02");
                }
                //车辆类型
                aa.setVehicleClassificationCode(returnList.get(i).get(10));
                //标准载重 36.00
                aa.setVehicleTonnage(returnList.get(i).get(11)+"0");
                //道路运输证号   位数不同不能上传
                aa.setRoadTransportCertificateNumber(returnList.get(i).get(12));
                //货物名称
                aa.setDescriptionOfGoods(returnList.get(i).get(13));
                //货物类型分类代码
                if (returnList.get(i).get(14).contains(".")){
                    String carGoType[]=returnList.get(i).get(14).split("\\.");
                    aa.setCargoTypeClassificationCode(carGoType[0]);
                 //   System.out.println(returnList.get(i).get(14));
                 //   System.out.println(carGoType[0]);
                }else{
                    aa.setCargoTypeClassificationCode(returnList.get(i).get(14));
                 //   System.out.println("此数据不包含...");
                }
                //货物重量
                aa.setGoodsItemGrossWeight(returnList.get(i).get(15)+"00");
                //体积
                aa.setCube(returnList.get(i).get(16)+"000");
                //打款编号
                aa.setSequenceCode(returnList.get(i).get(17));
                //司机钱
                if(returnList.get(i).get(18).length()<=7){
                    aa.setMonetaryAmount(returnList.get(i).get(18)+"00");
                }else{
                    aa.setMonetaryAmount(returnList.get(i).get(18)+"");
                }

                //打款时间

                if(returnList.get(i).get(19).contains(".")){
                    String shuzu[]=returnList.get(i).get(19).split("\\.");
                    aa.setDateTime(shuzu[0]);
                }else{
                    aa.setDateTime(returnList.get(i).get(19));
                }

                aa.setType(0);
                aa.setShippingNoteNumber(num);
                aaaService.save(aa);
                System.out.println(i);

            }
            //type
            //托运单号
        } catch (NumberFormatException e) {
            System.out.println(e);
        } catch (IOException e){
            System.out.println(e);
        }
      //  aaaService.save(aa);
        return "保存数据库成功";
    }
    @RequestMapping("/updatemoney")
    public String updateMoney(){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("type",0);
        double zhongliang;
        double money;
        List<Aaa> list=aaaService.selectByMap(map);
        DecimalFormat df=new DecimalFormat("#.000");
        for (int i=0; i<list.size();i++){
            Aaa aaa=new Aaa();
            aaa.setId(list.get(i).getId());
           // money=Double.valueOf(list.get(i).getTotalMonetaryAmount())-350.00;
          //  aaa.setMonetaryAmount(df.format(money)+"");
          //   12345.000 length=9
            if(list.get(i).getTotalMonetaryAmount().contains("P")){
                String splitString[]=list.get(i).getTotalMonetaryAmount().split("\\*");
                if(splitString[1].contains("-")){
                    String twosplit[]=splitString[1].split("\\-");
                    zhongliang=Double.valueOf(list.get(i).getGoodsItemGrossWeight());
                        System.out.println("重量:"+zhongliang+", 倍数："+twosplit[0]+",差价:"+twosplit[1]);
                        money=zhongliang*Double.valueOf(twosplit[0])-Double.valueOf(twosplit[1]);
                        System.out.println("货主给的钱="+df.format(money));
                        aaa.setTotalMonetaryAmount(df.format(money)+"");
                }else{
                    zhongliang=Double.valueOf(list.get(i).getGoodsItemGrossWeight());
                    money=zhongliang*Double.valueOf(splitString[1]);
                    System.out.println("货主给的钱="+df.format(money));
                    aaa.setTotalMonetaryAmount(df.format(money)+"");
                }
            }
            if(list.get(i).getMonetaryAmount().length()>9){
                String splitString[]=list.get(i).getMonetaryAmount().split("\\*");
                if(splitString[1].contains("-")){
                    String twosplit[]=splitString[1].split("\\-");
                    zhongliang=Double.valueOf(list.get(i).getGoodsItemGrossWeight());

                    if(twosplit.length==2){
                        System.out.println("重量:"+zhongliang+", 倍数："+twosplit[0]+",差价:"+twosplit[1]);
                        money=zhongliang*Double.valueOf(twosplit[0])-Double.valueOf(twosplit[1]);
                        aaa.setMonetaryAmount(df.format(money)+"");
                    }else if(twosplit.length==1){
                        money=zhongliang*Double.valueOf(twosplit[0]);
                        System.out.println("司机运费="+df.format(money));
                        aaa.setMonetaryAmount(df.format(money)+"");
                    }


                }
            }
                if(aaa.getTotalMonetaryAmount()!=null||aaa.getMonetaryAmount()!=null){
                    aaaService.updateById(aaa);
                }


        }
        return "update over!";
    }

}
